<?php
// +----------------------------------------------------------------------
// | CLTPHP.Pro [会员组设置]
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2029 https://www.cltphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://www.cltphp.com/detail-140.html )
// +----------------------------------------------------------------------
// | Author: chichu <1109305987@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\UserLevel as M;
use think\facade\Request;
use think\facade\Db;
use think\facade\View;

class UserLevel extends Base
{
    //会员组列表
    public function index(){
        if(Request::isAjax()) {
            //排序
            $list = M::order('sort','asc')->select();
            return ['code'=>0,'msg'=>'获取成功！','data'=>$list];
        }else{
            return view();
        }
    }
    //添加
    public function add(){
        if(request()->isPost()){
            $data=input('post.');
            $result = $this->validate($data, 'UserLevel');
            if (true !== $result) {
                return ['code'=>1,'msg'=>$result];
            } else {
                $result = M::create($data);
                if ($result) {
                    return ['code'=>0,'msg'=>'用户组添加成功'];
                } else {
                    return ['code'=>1,'msg'=>'用户组添加失败'];
                }
            }
        }else{
            return view('form',['info' => '']);
        }
    }
    //编辑
    public function edit(){
        if(request()->isPost()) {
            $data=input('post.');
            $result = M::editSave($data);
            return $result;
        }else{
            $level_id = input('level_id');
            $info = M::getInfo($level_id);
            return view('form',['info' =>$info]);
        }
    }
    //字段值修改
    public function cf(){
        $data = input('post.');
        $result = M::editSave($data);
        return $result;
    }
    //删除会员组
    function del($level_id) {
        M::destroy($level_id);
        return ['code'=>0,'msg'=>'删除成功!'];
    }

}